package com.ecgsystem.writelog;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Calendar;
import android.os.Environment;
import android.util.Log;

import com.ecgsystem.activity.OnlineECG;
import com.ecgsystem.constants.*;

public class WriteLog {
	private File myFile;
	private BufferedWriter buff;
	private String format;
	private String time;

	public WriteLog() {
		myFile = new File(Environment.getExternalStorageDirectory().getPath()
				+ "/ECGlog.xml");
		format = "<session>" + "	<time>%s</time>" + "	<total>%s</total>"
				+ "	<patientID>%s</patientID>" + "	<draw>%s</draw>"
				+ "	<loss_percentage>%s</loss_percentage>" + "</session>";
		time = java.text.DateFormat.getDateTimeInstance().format(
				Calendar.getInstance().getTime());
	}

	public void write() {
		if (!myFile.exists())
			try {
				myFile.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		try {
			buff = new BufferedWriter(new FileWriter(myFile, true));

			if (Commons.DRAWCOUNT != 0 && Commons.LEADCOUNT != 0
					&& Commons.TOTAL_PACKET != 0) {
				float packets = Commons.DRAWCOUNT / Commons.LEADCOUNT;
				Log.d("Draw: ", "" + packets);
				buff.append(String.format(format, time, Commons.TOTAL_PACKET,
						Commons.PATIENTID, packets, 100f - 100 * packets
								/ Commons.TOTAL_PACKET + "%"));
				buff.newLine();
				buff.close();
				Log.d("LOG" , "Done");
				Log.d("Queue", OnlineECG.QueueDraw[0].size()  + "");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}